package com.education.common.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.util.Auth;

import java.util.List;

/**
 * @author zjz
 */
public class QiNiuAfterUtil {

    private static Configuration cfg = new Configuration(Region.region0());

    //...其他参数参考类注释

    private static String accessKey = "TWDj6oF-ZkhXLmn8Iipc0woi6teoq-jerUpwuW3s";
    private static String secretKey = "S6FGTu-FfkSIoii0k9jUwRz2S5Xlac7VhVl_cELy";

    private static Auth auth = Auth.create(accessKey, secretKey);
    private static BucketManager bucketManager = new BucketManager(auth, cfg);

    @SuppressWarnings("all")
    public static void test(List<String> urlList){
        try {
            //单次批量请求的文件数量不得超过1000

            BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
            for (String key : urlList) {
                String toBucket = "shix";
                String fromBucket = "reource";
                batchOperations.addCopyOp(fromBucket, key, toBucket, key);
            }

            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);

            for (int i = 0; i < urlList.size(); i++) {
                BatchStatus status = batchStatusList[i];
                String key = urlList.get(i);
                System.out.print(key + "\t");
                if (status.code == 200) {
                    System.out.println("copy success");
                } else {
                    System.out.println(status.data.error);
                }
            }
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
    }


}
